package com.moshang.mall.member.service.impl;

import com.moshang.mall.member.vo.MemberLoginVo;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moshang.mall.utils.PageUtils;
import com.moshang.mall.utils.Query;

import com.moshang.mall.member.dao.MemberDao;
import com.moshang.mall.member.entity.MemberEntity;
import com.moshang.mall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public MemberEntity login(MemberLoginVo vo) {
        String loginAccount=vo.getLoginAccount();
        String password=vo.getPassword();
        MemberEntity member=this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username",loginAccount).or().eq("mobile",loginAccount)
        .or().eq("email",loginAccount));
        if (member==null){
            return null;
        }
        String dbPassword=member.getPassword();
        BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
        boolean matches = passwordEncoder.matches(password, dbPassword);
        if (matches){
            return member;
        }
        return null;
    }

}